from django.utils.deprecation import MiddlewareMixin
class MyMiddleWareTest(MiddlewareMixin):
    pass
    # 实现方法
    # def process_request(self, request):
    #     """
    #     请求到达之后第一个被触发，
    #     :param request: 包含请求信息的请求对象
    #     :return: 如果返回None或者没有返回值，代表通过当前方法，继续向后执行，否则直接返回 """
    #     print("process_request")
    #     ## IP黑名单 10.10.120.165 174 123
    #     ip_list = ["10.10.120.165", "10.10.120.174", "10.10.120.123"]
    #     # 获取到访问者的ip
    #     ac_ip = request.META.get("REMOTE_ADDR")
    #     print(ac_ip)
    #     # 判断 访问者的ip 是否在黑名单中
    #     from django.shortcuts import render_to_response
    #     if ac_ip in ip_list:
    #         return render_to_response("store/404.html")
    #
    # def process_view(self, request, callback, callargs, callkwargs):
    #     """
    #     process_request 返回None或者没有返回值的时候触发，触发之后进入视图
    #     :param request: 包含请求信息的请求对象
    #     :param callback: 要执行的视图
    #     :param callargs: 元祖 通过组匹配传递的参数，位置参数
    #     :param callkwargs: 字典 通过组匹配+别名传递的参数，关键字参数
    #     :return: 如果返回None 或者没有返回值，继续向后执行
    #     """
    #     print("process view")
    #     # print(callback)
    #     # print(callargs)
    #     # print(callkwargs)
    #     # 判断版本号
    #     from django.http import HttpResponse
    #     version = callkwargs.get("version")
    #     if version != "v1":
    #         return HttpResponse("访问版本不对！！")
    #
    # def process_exception(self,request,exception):
    #     """
    #     视图出现异常或报错 被触发
    #     :param request: 包含请求信息的请求对象
    #     :param exception: 出现的异常信息对象
    #     :return:
    #     """
    #     print("process exception")
    #     print(exception)
    #     # 处理错误 编写日志
    #     # 写文件
    #     # 格式化
    #     from datetime import datetime
    #     data = "【%s】%s \n" %(datetime.strftime(datetime.now(),
    #                            "%Y-%m-%d %H:%M:%S"),str(exception))
    #     with open("error.log","a",encoding="utf-8") as f:
    #         f.write(data)
    #     from django.shortcuts import render_to_response
    #     return render_to_response("store/404.html")
    #
    # def process_template_response(self,request,response):
    #     print("process template exception")
    #     return response

    # def process_response(self,request,response):
    #     """
    #     每次视图return之后被触发
    #     :param request: 包含请求信息的请求对象
    #     :param response: 响应对象
    #     :return: 返回响应对象
    #     """
    #     # 统一处理响应对象
    #     print("process response")
    #     response.set_cookie("v","v1")
    #     return response
